package com.pitb.citizenreporting;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpPostUtil {
	public static final String COMPLAINT_URL = "http://securefy.com:8080/watsan/register_complaints.php?";
	public static final String MONITOR_COMPLAINT_URL = "http://securefy.com:8080/watsan/monitor_complaints.php?";

	// public static final String COMPLAINT_URL =
	// "http://192.168.15.218:8080/watsan/register_complaints.php?";

	public static String postComplaint(String problem, String address,
			String phone, String province, String city, String area, String type) {
		try {
			StringBuilder stringBuilder = new StringBuilder();
			problem = URLEncoder.encode(problem, "utf-8");
			address = URLEncoder.encode(address, "utf-8");
			phone = URLEncoder.encode(phone, "utf-8");
			province = URLEncoder.encode(province, "utf-8");
			city = URLEncoder.encode(city, "utf-8");
			area = URLEncoder.encode(area, "utf-8");
			type = URLEncoder.encode(type, "utf-8");
			String url = COMPLAINT_URL + "problem=" + problem + "&address="
					+ address + "&phone=" + phone + "&province=" + province
					+ "&city=" + city + "&area=" + area + "&type=" + type;
			HttpPost httppost = new HttpPost(url);
			HttpClient client = new DefaultHttpClient();
			HttpResponse response;
			stringBuilder = new StringBuilder();
			response = client.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream stream = entity.getContent();
			int b;
			while ((b = stream.read()) != -1) {
				stringBuilder.append((char) b);
			}
			return stringBuilder.toString();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String trackComplaint(String token) {
		try {
			StringBuilder stringBuilder = new StringBuilder();
			token = URLEncoder.encode(token, "utf-8");			
			String url = MONITOR_COMPLAINT_URL + "token=" + token ;
			HttpPost httppost = new HttpPost(url);
			HttpClient client = new DefaultHttpClient();
			HttpResponse response;
			stringBuilder = new StringBuilder();
			response = client.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream stream = entity.getContent();
			int b;
			while ((b = stream.read()) != -1) {
				stringBuilder.append((char) b);
			}
			return stringBuilder.toString();
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}
}
